// import request from '../utils/request';
import axios from "axios";
const Qs = require('qs');
const apiUrl="http://127.0.0.1:8081/";
const api = {
    apiUrl:"http://127.0.0.1:8081/",
    post: (url, args) => {
        // console.log(url,args)
        if(url.startsWith("/"))
            url=url.substring(1,url.length)

        return new Promise((resolve,) => {
            axios({
                url: apiUrl + url,
                method: 'post',
                data: args,
                headers:{
                    "Content-Type":"application/json;charset=utf-8"
                    // "Content-Type":"application/x-www-form-urlencode;charset=utf-8"
                }
            }).then(res => {
                resolve(res.data);
                // console.log('请求结果：', res);
            }).catch(res => {
                if(res.toString().indexOf('401')!=-1){
                    window.parent.location.href="/login.html"
                }
                resolve(res.data);
            });

        })
    },
     get: (url) => {
         if(url.startsWith("/"))
             url=url.substring(1,url.length)
         return new Promise((resolve,) => {
             axios({
                 url: apiUrl + url,
                 method: 'get',
                 headers:{
                 },
             }).then(res => {
                 resolve(res.data);
                 // console.log('请求结果：', res);
             }).catch(res => {
                 if(res.toString().indexOf('401')!=-1){
                     window.parent.location.href="/login.html"
                 }
                 resolve(res.data);
             });

         })
     },
     postbyhost: (host, url, args) => {
         return new Promise((resolve,) => {
             axios({
                 url: host + url,
                 method: 'post',
                 data: args,
                 headers:{
                     "Content-Type":"application/json;charset=utf-8"
                     // "Content-Type":"application/x-www-form-urlencode;charset=utf-8"
                 }

             }).then(res => {
                 resolve(res.data);
                 // console.log('请求结果：', res);
             }).catch(res => {
                 resolve(res.data);
             });

         })
     },
     // postJson: (host, url, args) => {
     //     return new Promise((resolve,) => {
     //         axios({
     //             url: host + url,
     //             method: 'post',
     //             data: args,
     //             headers:{
     //                 "Content-Type":"application/json;charset=utf-8"
     //             }
     //
     //         }).then(res => {
     //             resolve(res.data);
     //             // console.log('请求结果：', res);
     //         }).catch(res => {
     //             resolve(res.data);
     //         });
     //
     //     })
     // },
     getbyhost: (host, url) => {
         return new Promise((resolve,) => {
             axios({
                 url: host + url,
                 method: 'post'
             }).then(res => {
                 resolve(res.data);

                 // console.log('请求结果：', res);
             }).catch(res => {
                 resolve(res.data);
             });

         })
     }


 }
export default api;
